package view.inventory.menu
{
import core.Language;
import core.model.GameModel;

import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;

import framework.proxy.ComponentProxy;
import framework.proxy.IComponentProxy;
import framework.view.components.button.ButtonProxy;

import values.item.IItemValue;

/**
 * InventoryItemRenderMenuProxy
 * @author jliang
 *
 */
public class InventoryItemMenuProxy extends ComponentProxy
{
	//==========================================================================
	//  Constructor
	//==========================================================================
	public function InventoryItemMenuProxy()
	{
		super();
	}
	//==========================================================================
	//  Properties
	//==========================================================================
	protected var _data:IItemValue;

	public function get data():IItemValue
	{
		return _data;
	}

	public function set data(value:IItemValue):void
	{
		_data = value;
	}
	
	private var flag:Boolean = true;
	//==========================================================================
	//  Overridden methods: ComponentProxy
	//==========================================================================	
	override protected function get proxyMapping():Array
	{
		return [ 
			{ skin:"sellBtn", classRef: ButtonProxy, id:"sellBtn", text:Language.getText("@sell_button_label") ,onClick: sellHandler }
		];
	}
	
	override public function parse(target:MovieClip):void
	{
		super.parse(target);
		
		if (skin.stage)
		{
			skin.stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpOnStageHandler);
		}
		else
		{
			skin.addEventListener(Event.ADDED_TO_STAGE, skinAddToStageHandler);
		}
	}
	
	override public function dispose():void
	{
		if (skin.stage)
		{
			skin.stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUpOnStageHandler);
		}
		else
		{
			skin.removeEventListener(Event.ADDED_TO_STAGE, skinAddToStageHandler);
		}
		
		super.dispose();
	}
	//==========================================================================
	//  Event handlers
	//==========================================================================
	protected function sellHandler():void
	{
		GameModel.instance.playerModel.sellItem(data);
	}
	
	protected function skinAddToStageHandler(event:Event):void
	{
		skin.removeEventListener(Event.ADDED_TO_STAGE, skinAddToStageHandler);
		
		skin.stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpOnStageHandler);
	}
	
	protected function mouseUpOnStageHandler(event:Event):void
	{		
		if (flag) 
		{
			flag =!flag;
			return;
		}
		
		event.target.removeEventListener(event.type, mouseUpOnStageHandler);
		/*
		for each (var proxy:IComponentProxy in proxys)
		{
			if (proxy is ButtonProxy && ButtonProxy(proxy).onClick != null)
			{
				ButtonProxy(proxy).onClick();
			}
		}
		*/
		if (skin.parent)
		{
			skin.parent.removeChild(skin);
		}
		dispose();
	}
}
}




